package com.datastructure.test.foundmonotonestack;

public class FoundMonotoneStack {

    public static void main(String[] args) {
        FoundMonotoneStack foundMonotoneStack =new FoundMonotoneStack();
        int[] nums = {3,4,1,5,6,2,7};
        int[][] monotoneStack = foundMonotoneStack.foundMonotoneStack(nums);
        System.out.println(monotoneStack);
    }

    public int[][] foundMonotoneStack (int[] nums) {
        // write code here
        if(nums==null||nums.length==1){
            return null;
        }
        int[][] result = new int[nums.length][2];
        for (int i = 0; i < nums.length; i++) {
            computeFoundMonotoneStack(i,nums,result);
        }
        return result;
    }

    private void computeFoundMonotoneStack(int start, int[] nums, int[][] result) {
        result[start][0]=-1;
        result[start][1]=-1;
        int length = nums.length;
        int left = start-1,right = start+1;
        while (left>=0){
            if(nums[left]<nums[start]){
                result[start][0]=left;
                break;
            }
            left--;
        }
        while (right<length){
            if(nums[right]<nums[start]){
                result[start][1]=right;
                break;
            }
            right++;
        }
    }
}
